#!/bin/bash
PWD_SV=$(pwd)
function git_lib() {
    #git clone http://10.0.0.66/autodev/kylinsecos/os_libs/lib.git
    source "./lib/utils.sh"
}
#参数帮助
service_help() {
    cat >&2 <<-EOF
	 ---------------------------------------
	| help   : ./service [OPTION]           |
	| OPTION :                              |
	|    ' '        : Run All Services      |
	|    -s svname  : Run One Service       |
	|    -c svname  : Clean One Env         |
	|    -t svname  : Run One Service Test  |
	 ---------------------------------------
	EOF
}

###        NFS服务       ###
function nfs_bak() {
    log_title "开始NFS服务搭建测试"
    local nfs_path="./nfs"
    if [ -f "/etc/exports.bak" ]; then
        cp_file "${nfs_path}/exports" "/etc/"
    else
        mv_file "/etc/exports" "/etc/exports.bak"
        cp_file "${nfs_path}/exports" "/etc/"
    fi
}

function nfs_build() {
    rpcinfo -p localhost | grep -E "111|2049"
    if [ "$?" == 0 ]; then
        rpcinfo -p localhost | grep -E "111|2049" | tee -a "${OUTPUT_FILE}"
    else
        log_error "RPC服务注册失败"
        exit 1
    fi

    stop_fw
    stop_se

    log_title "创建NFS共享只读目录：/home/nfs"
    mk_dir /home/nfs
    ls_file_dir /home/nfs | tee -a "${OUTPUT_FILE}"

    log_title "创建NFS共享用户及所属组读写目录：/home/upload"
    groupadd -g 210 nfs-upload
    useradd -g 210 -u 210 -M nfs-upload
    mk_dir /home/upload
    chown -R nfs-upload:nfs-upload /home/upload
    ls_file_dir /home/upload | tee -a "${OUTPUT_FILE}"

    log_title "创建NFS仅共享给客户端andy用户使用的读写目录/home/andy：useradd andy"
    cat /etc/passwd | grep "andy"
    if [ "$?" != 0 ]; then
        useradd andy
    fi
    ls_file_dir /home/andy | tee -a "${OUTPUT_FILE}"
    echo "127.0.0.1   localhost localhost.localdomain" >>/etc/hosts
}

function nfs_start() {
    local packages=("nfs-server" "rpcbind")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function nfs_test() {
    log_title "NFS服务测试是否成功:showmount -e localhost"
    showmount -e localhost
    if [ "$?" == 0 ]; then
        showmount -e localhost | tee -a "${OUTPUT_FILE}"
        log_title "NFS搭建与测试成功"
        log_ret "NFS搭建与测试成功" "TPASS"
    else
        log_error "NFS搭建与测试失败"
        log_ret "NFS搭建与测试失败" "TFAIL"
    fi
}

function nfs_clean() {
    if [ -f "/etc/exports.bak" ]; then
        rm_file_dir "/etc/exports"
        rm_file_dir "/home/nfs"
        rm_file_dir "/home/upload"
        del_user andy
        mv_file "/etc/exports.bak" "/etc/exports"
    fi
    log_title "清理并结束NFS服务搭建测试"
}

function nfs_run() {
    local param="$1"
    sv_name_rpm "${param}"
    nfs_bak
    nfs_build
    nfs_start
    nfs_test
    #nfs_clean
}

###        HTTP服务       ###
function http_bak() {
    log_title "开始HTTP服务搭建测试"
    local http_path="./http"
    if [ -f "/etc/httpd/conf/httpd.conf.bak" ]; then
        cp_file "${http_path}/httpd.conf" "/etc/httpd/conf/"
    else
        mv_file "/etc/httpd/conf/httpd.conf" "/etc/httpd/conf/httpd.conf.bak"
        cp_file "${http_path}/httpd.conf" "/etc/httpd/conf/"
    fi
}

function http_build() {
    stop_fw
    stop_se
}

function http_start() {
    systemctl status nginx | grep ": active"
    if [ "$?" == 0 ]; then
        st_stop "nginx"
    fi
    local packages=("httpd")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done

    netstat -ltpnu | grep 80

}

function http_test() {
    log_title "HTTP服务测试是否成功:curl http://localhost|grep 'Apache HTTP Server'"
    curl http://localhost | grep "Apache HTTP Server"
    if [ "$?" == 0 ]; then
        curl http://localhost | grep "Apache HTTP Server" | tee -a "${OUTPUT_FILE}"
        log_title "HTTP搭建与测试成功"
        log_ret "HTTP搭建与测试成功" "TPASS"
    else
        log_error "HTTP搭建与测试失败"
        log_ret "HTTP搭建与测试失败" "TFAIL"
    fi
}

function http_clean() {
    if [ -f "/etc/httpd/conf/httpd.conf.bak" ]; then
        rm_file_dir "/etc/httpd/conf/httpd.conf"
        mv_file "/etc/httpd/conf/httpd.conf.bak" "/etc/httpd/conf/httpd.conf"
    fi
    log_title "清理并结束HTTP服务搭建测试"
}

function http_run() {
    local param="$1"
    sv_name_rpm "${param}"
    http_bak
    http_build
    http_start
    http_test
    #http_clean
}

###        HTTPS服务       ###
function https_bak() {
    log_title "开始HTTPS服务搭建测试"
    local https_path="./https"
    if [ -f "/etc/pki/tls/openssl.cnf.bak" ]; then
        cp_file "${https_path}/openssl.cnf" "/etc/pki/tls/"
    else
        mv_file "/etc/pki/tls/openssl.cnf" "/etc/pki/tls/openssl.cnf.bak"
        cp_file "${https_path}/openssl.cnf" "/etc/pki/tls/"
    fi
    if [ -f "/etc/nginx/nginx.conf.bak" ]; then
        cp_file "${https_path}/nginx.conf" "/etc/nginx/"
    else
        mv_file "/etc/nginx/nginx.conf" "/etc/nginx/nginx.conf.bak"
        cp_file "${https_path}/nginx.conf" "/etc/nginx/"
    fi
    if [ -d "/root/CA" ]; then
        rm_file_dir "/root/CA"
    fi
    if [ -d "/etc/pki/CA/CSR" ]; then
        rm_file_dir "/etc/pki/CA/CSR"
    fi
}

function https_build() {
    #http服务存在需要关闭，不存在启动nginx即可
    local pg_httpd=("httpd")
    for i in "${pg_httpd[@]}"; do
        rpm -qa | grep "${i}"
        if [ "$?" == 0 ]; then
            st_stop "${i}"
        fi
    done

    #生成自签名证书
    touch /etc/pki/CA/index.txt
    echo "03" >/etc/pki/CA/serial
    cd /etc/pki/CA
    umask 066
    openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048
    openssl req -new -x509 -subj "/C=ZH/ST=ZH/L=ZH/O=ZH/OU=ZH/CN=ZH" -batch -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem
    openssl x509 -subject -dates -fingerprint -noout -in cacert.pem
    cd -

    #生成CA证书申请文件
    mk_dir /root/CA
    cd /root/CA
    umask 066
    openssl genrsa -out service.key 2048
    openssl req -new -subj "/C=ZH/ST=ZH/L=ZH/O=ZH/OU=ZH/CN=ZH" -batch -key service.key -out service.csr
    #CA签署证书，并颁发给请求者
    mk_dir /etc/pki/CA/CSR
    mv service.csr /etc/pki/CA/CSR
    openssl ca -in /etc/pki/CA/CSR/service.csr -batch -out /etc/pki/CA/certs/service.crt -days 365
    cd -
}

function https_start() {
    local packages=("nginx")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
    nginx -s reload
}

function https_test() {
    log_title "HTTPS服务测试是否成功:curl -v https://localhost |grep 'successfully'"
    curl -v https://localhost
    if [ "$?" == 60 ]; then
        curl -v https://localhost | tee -a "${OUTPUT_FILE}"
        log_title "HTTPS搭建与测试成功"
        log_ret "HTTPS搭建与测试成功" "TPASS"
    else
        log_error "HTTPS搭建与测试失败"
        log_ret "HTTPS搭建与测试失败" "TFAIL"
    fi
}

function https_clean() {
    if [ -f "/etc/pki/tls/openssl.cnf.bak" ]; then
        rm_file_dir "/etc/pki/tls/openssl.cnf"
        mv_file "/etc/pki/tls/openssl.cnf.bak" "/etc/pki/tls/openssl.cnf"
    fi
    if [ -f "/etc/nginx/nginx.conf.bak" ]; then
        rm_file_dir "/etc/nginx/nginx.conf"
        mv_file "/etc/nginx/nginx.conf.bak" "/etc/nginx/nginx.conf"
    fi
    if [ -d "/root/CA" ]; then
        rm_file_dir "/root/CA"
    fi
    if [ -d "/etc/pki/CA/CSR" ]; then
        rm_file_dir "/etc/pki/CA/CSR"
    fi

    log_title "清理并结束HTTPS服务搭建测试"
}

function https_run() {
    local param="$1"
    sv_name_rpm "${param}"
    https_bak
    https_build
    https_start
    https_test
    #https_clean
}

###        FTP服务       ###
function ftp_bak() {
    log_title "开始FTP服务搭建测试"
    local ftp_path="./ftp"
    if [ -f "/etc/vsftpd/vsftpd.conf.bak" ]; then
        cp_file "${ftp_path}/vsftpd.conf" "/etc/vsftpd/"
    else
        mv_file "/etc/vsftpd/vsftpd.conf" "/etc/vsftpd/vsftpd.conf.bak"
        cp_file "${ftp_path}/vsftpd.conf" "/etc/vsftpd/"
    fi
}

function ftp_build() {
    stop_fw
    stop_se

    # groupadd ftpgroup
    # useradd -g ftpgroup -d /opt/ftp -s /sbin/nologin -m ftpuser1
    # set_pw "ftpuser1"

    chmod 777 /var/ftp/pub
}

function ftp_start() {
    local packages=("vsftpd")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function ftp_test() {
    log_title "FTP服务测试是否成功:curl ftp://localhost"
    curl ftp://localhost
    if [ "$?" == 0 ]; then
        curl ftp://localhost | tee -a "${OUTPUT_FILE}"
        log_title "FTP搭建与测试成功"
        log_ret "FTP搭建与测试成功" "TPASS"
    else
        log_error "FTP搭建与测试失败"
        log_ret "FTP搭建与测试失败" "TFAIL"
    fi
}

function ftp_clean() {
    if [ -f "/etc/vsftpd/vsftpd.conf.bak" ]; then
        rm_file_dir "/etc/vsftpd/vsftpd.conf"
        mv_file "/etc/vsftpd/vsftpd.conf.bak" "/etc/vsftpd/vsftpd.conf"
    fi
    log_title "清理并结束FTP服务搭建测试"
}

function ftp_run() {
    local param="$1"
    sv_name_rpm "${param}"
    ftp_bak
    ftp_build
    ftp_start
    ftp_test
    #ftp_clean
}

###        SMB服务       ###
function smb_bak() {
    log_title "开始SMB服务搭建测试"
    local smb_path="./smb"
    if [ -f "/etc/samba/smb.conf.bak" ]; then
        cp_file "${smb_path}/smb.conf" "/etc/samba"
    else
        mv_file "/etc/samba/smb.conf" "/etc/samba/smb.conf.bak"
        cp_file "${smb_path}/smb.conf" "/etc/samba"
    fi
    if [ -d "/smb" ]; then
        rm_file_dir "/smb"
    fi
}

function smb_build() {
    stop_fw
    stop_se

    groupadd user
    useradd -g user user1
    useradd -g user user2
    useradd -g user user3
    ls_file_dir "/home/user1"
    ls_file_dir "/home/user2"
    ls_file_dir "/home/user3"

    set_pw "user1"
    set_pw "user2"
    set_pw "user3"

    mk_dir "/smb"
    chgrp user /smb/
    chmod g+w /smb
    ls_file_dir /smb
}

function smb_start() {
    local packages=("smb")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function smb_test() {
    log_title "SMB服务测试是否成功:smbclient //localhost/tmp -N"
    ls_file_dir /tmp
    ls_file_dir "/home/user1"
    ls_file_dir "/home/user2"
    ls_file_dir "/home/user3"
    ls_file_dir "/smb"
    systemctl status smb | grep ": active"
    if [ "$?" == 0 ]; then
        log_title "SMB搭建与测试成功"
        log_ret "SMB搭建与测试成功" "TPASS"
    else
        log_error "SMB搭建与测试失败"
        log_ret "SMB搭建与测试失败" "TFAIL"
    fi
}

function smb_clean() {
    if [ -f "/etc/samba/smb.conf.bak" ]; then
        rm_file_dir "/etc/samba/smb.conf"
        mv_file "/etc/samba/smb.conf.bak" "/etc/samba/smb.conf"
    fi
    if [ -d "/smb" ]; then
        rm_file_dir "/smb"
    fi
    log_title "清理并结束SMB服务搭建测试"
}

function smb_run() {
    local param="$1"
    sv_name_rpm "${param}"
    smb_bak
    smb_build
    smb_start
    smb_test
    #smb_clean
}

###        LDAP服务       ###
function ldap_bak() {
    log_title "开始LDAP服务搭建测试"
    local ldap_path="./ldap"
    if [ -f "/etc/openldap/ldap.conf.bak" ]; then
        cp_file "${ldap_path}/ldap.conf" "/etc/openldap/"
    else
        mv_file "/etc/openldap/ldap.conf" "/etc/openldap/ldap.conf.bak"
        cp_file "${ldap_path}/ldap.conf" "/etc/openldap/"
    fi
    if [ -f "/etc/pki/tls/openssl.cnf.bak" ]; then
        cp_file "${ldap_path}/openssl.cnf" "/etc/pki/tls/"
    else
        mv_file "/etc/pki/tls/openssl.cnf" "/etc/pki/tls/openssl.cnf.bak"
        cp_file "${ldap_path}/openssl.cnf" "/etc/pki/tls/"
    fi
    if [ -f "/etc/openldap/slapd.d/cn=config/olcDatabase={2}mdb.ldif.bak" ]; then
        cp_file "${ldap_path}/olcDatabase={2}mdb.ldif" "/etc/openldap/slapd.d/cn=config/"
    else
        mv_file "/etc/openldap/slapd.d/cn=config/olcDatabase={2}mdb.ldif" "/etc/openldap/slapd.d/cn=config/olcDatabase={2}mdb.ldif.bak"
        cp_file "${ldap_path}/olcDatabase={2}mdb.ldif" "/etc/openldap/slapd.d/cn=config/"
    fi
    if [ -f "/etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif.bak" ]; then
        cp_file "${ldap_path}/olcDatabase={1}monitor.ldif" "/etc/openldap/slapd.d/cn=config/"
    else
        mv_file "/etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif" "/etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif.bak"
        cp_file "${ldap_path}/olcDatabase={1}monitor.ldif" "/etc/openldap/slapd.d/cn=config/"
    fi
}

function ldap_build() {
    stop_se
    stop_fw

    local pwd=$(pwd)
    local ldap_path="ldap"
    cd /root
    cp_file "${pwd}/${ldap_path}/changepwd.ldif" "./"
    systemctl start slapd
    if [ "$?" != 0 ]; then
        log_error "LDAP启动服务失败"
        exit 1
    fi
    ldapadd -Y EXTERNAL -H ldapi:/// -f changepwd.ldif

    cp_file "${pwd}/${ldap_path}/ldap_test.sh" "./"
    chmod 755 ./ldap_test.sh
    ./ldap_test.sh

    cp_file "${pwd}/${ldap_path}/changedomain.ldif" "./"
    ldapmodify -Y EXTERNAL -H ldapi:/// -f changedomain.ldif

    cp_file "${pwd}/${ldap_path}/add-memberof.ldif" "./"
    cp_file "${pwd}/${ldap_path}/refint1.ldif" "./"
    cp_file "${pwd}/${ldap_path}/refint2.ldif" "./"

    ldapadd -Q -Y EXTERNAL -H ldapi:/// -f add-memberof.ldif
    ldapmodify -Q -Y EXTERNAL -H ldapi:/// -f refint1.ldif
    ldapadd -Q -Y EXTERNAL -H ldapi:/// -f refint2.ldif

    cp_file "${pwd}/${ldap_path}/base.ldif" "./"

    cd -

}

function ldap_start() {
    #客户端验证配置
    #local packages=("slapd" "nslcd" "sssd")
    local packages=("slapd")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function ldap_test() {
    log_title "slapd服务状态如下："
    systemctl status slapd | grep ": active"
    log_title "添加LDAP对象："
    local ldap_passwd="123456"
    ldapadd -x -D cn=admin,dc=yaobili,dc=com -w "${ldap_passwd}" -f /root/base.ldif
    log_title "详细数据库信息如下："
    ldapsearch -x -b "dc=yaobili,dc=com"
    if [ "$?" == 0 ]; then
        ldapsearch -x -b "dc=yaobili,dc=com" | tee -a "${OUTPUT_FILE}"
        log_title "LDAP搭建与测试成功"
        log_ret "LDAP搭建与测试成功" "TPASS"
    else
        log_error "LDAP搭建与测试失败"
        log_ret "LDAP搭建与测试失败" "TFAIL"
    fi

}

function ldap_client() {
    log_title "客户端验证配置"
    local ldap_path="./ldap"
    #客户端验证配置
    if [ -f "/etc/nslcd.conf.bak" ]; then
        cp_file "${ldap_path}/nslcd.conf" "/etc/"
    else
        mv_file "/etc/nslcd.conf" "/etc/nslcd.conf.bak"
        cp_file "${ldap_path}/nslcd.conf" "/etc/"
    fi
    if [ -f "/etc/nsswitch.conf.bak" ]; then
        cp_file "${ldap_path}/nsswitch.conf" "/etc/"
    else
        mv_file "/etc/nsswitch.conf" "/etc/nsswitch.conf.bak"
        cp_file "${ldap_path}/nsswitch.conf" "/etc/"
    fi
    if [ -f "/etc/pam.d/vsftpd.bak" ]; then
        cp_file "${ldap_path}/vsftpd" "/etc/pam.d/"
    else
        mv_file "/etc/pam.d/vsftpd" "/etc/pam.d/vsftpd.bak"
        cp_file "${ldap_path}/vsftpd" "/etc/pam.d/"
    fi
    if [ -f "/etc/pam.d/password-auth.bak" ]; then
        cp_file "${ldap_path}/password-auth" "/etc/pam.d/"
    else
        mv_file "/etc/pam.d/password-auth" "/etc/pam.d/password-auth.bak"
        cp_file "${ldap_path}/password-auth" "/etc/pam.d/"
    fi
    if [ -f "/etc/pam.d/system-auth.bak" ]; then
        cp_file "${ldap_path}/system-auth" "/etc/pam.d/"
    else
        mv_file "/etc/pam.d/system-auth" "/etc/pam.d/system-auth.bak"
        cp_file "${ldap_path}/system-auth" "/etc/pam.d/"
    fi
    if [ -f "/etc/sssd/sssd.conf.bak" ]; then
        cp_file "${ldap_path}/sssd.conf" "/etc/sssd/"
    else
        mv_file "/etc/sssd/sssd.conf" "/etc/sssd/sssd.conf.bak"
        cp_file "${ldap_path}/sssd.conf" "/etc/sssd/"
    fi
    if [ -f "/etc/pki/tls/openssl.cnf.bak" ]; then
        cp_file "${ldap_path}/openssl.cnf" "/etc/pki/tls/"
    else
        mv_file "/etc/pki/tls/openssl.cnf" "/etc/pki/tls/openssl.cnf.bak"
        cp_file "${ldap_path}/openssl.cnf" "/etc/pki/tls/"
    fi

    #生成自签名证书
    touch /etc/pki/CA/index.txt
    echo "03" >/etc/pki/CA/serial
    cd /etc/pki/CA
    umask 066
    openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048
    openssl req -new -x509 -subj "/C=ZH/ST=ZH/L=ZH/O=ZH/OU=ZH/CN=ZH" -batch -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem
    openssl x509 -subject -dates -fingerprint -noout -in cacert.pem
    cd -

    #生成CA证书申请文件
    mk_dir /root/CA
    cd /root/CA
    umask 066
    openssl genrsa -out service.key 2048
    openssl req -new -subj "/C=ZH/ST=ZH/L=ZH/O=ZH/OU=ZH/CN=ZH" -batch -key service.key -out service.csr
    #CA签署证书，并颁发给请求者
    mk_dir /etc/pki/CA/CSR
    mv service.csr /etc/pki/CA/CSR
    openssl ca -in /etc/pki/CA/CSR/service.csr -batch -out /etc/pki/CA/certs/service.crt -days 365
    cd -

    mk_dir /etc/openldap/cacerts
    cp_file "/etc/pki/CA/cacert.pem" "/etc/openldap/cacerts/"
    cp_file "/root/CA/service.key" "/etc/openldap/certs"
    cp_file "/etc/pki/CA/CSR/service.csr" "/etc/openldap/certs"
    cp_file "/etc/pki/CA/certs/service.crt" "/etc/openldap/certs"

    echo "${ser_ip}  yaobili.com" >/etc/hosts
    local ser_ip="$1"
    sed -i "/uri ldap:\/\/10.90.21.119/a\
uri ldap:\/\/${ser_ip}" /etc/nslcd.conf
    sed -i "/ldap_search_base = dc=yaobili,dc=com/a\
ldap_uri = ldap:\/\/${ser_ip}" /etc/sssd/sssd.conf

    chmod 600 /etc/nslcd.conf
    chmod 600 /etc/sssd/sssd.conf
    local packages=("nslcd" "sssd")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done

    log_title "nslcd服务状态如下："
    systemctl status nslcd | grep ": active"
    log_title "sssd服务状态如下："
    systemctl status sssd | grep ": active"
    log_title "查看ldaptest用户信息："
    getent passwd | grep ldaptest
    id ldaptest
    log_title "命令登录ldaptest用户："
    su - ldaptest
}

function ldap_clean() {
    if [ -f "/etc/openldap/slapd.d/cn=config/olcDatabase={2}mdb.ldif.bak" ]; then
        rm_file_dir "/etc/openldap/slapd.d/cn=config/olcDatabase={2}mdb.ldif"
        mv_file "/etc/openldap/slapd.d/cn=config/olcDatabase={2}mdb.ldif.bak" "/etc/openldap/slapd.d/cn=config/olcDatabase={2}mdb.ldif"
    fi
    if [ -f "/etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif.bak" ]; then
        rm_file_dir "/etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif"
        mv_file "/etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif.bak" "/etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif"
    fi
    if [ -f "/etc/openldap/ldap.conf.bak" ]; then
        rm_file_dir "/etc/openldap/ldap.conf"
        mv_file "/etc/openldap/ldap.conf.bak" "/etc/openldap/ldap.conf"
    fi
    if [ -f "/etc/pki/tls/openssl.cnf.bak" ]; then
        rm_file_dir "/etc/pki/tls/openssl.cnf"
        mv_file "/etc/pki/tls/openssl.cnf.bak" "/etc/pki/tls/openssl.cnf"
    fi

    #客户端清理
    if [ -f "/etc/nslcd.conf.bak" ]; then
        rm_file_dir "/etc/nslcd.conf"
        mv_file "/etc/nslcd.conf.bak" "/etc/nslcd.conf"
    fi
    if [ -f "/etc/nsswitch.conf.bak" ]; then
        rm_file_dir "/etc/nsswitch.conf"
        mv_file "/etc/nsswitch.conf.bak" "/etc/nsswitch.conf"
    fi
    if [ -f "/etc/pam.d/password-auth.bak" ]; then
        rm_file_dir "/etc/pam.d/password-auth"
        mv_file "/etc/pam.d/password-auth.bak" "/etc/pam.d/password-auth"
    fi
    if [ -f "/etc/pam.d/system-auth.bak" ]; then
        rm_file_dir "/etc/pam.d/system-auth"
        mv_file "/etc/pam.d/system-auth.bak" "/etc/pam.d/system-auth"
    fi
    if [ -f "/etc/sssd/sssd.conf.bak" ]; then
        rm_file_dir "/etc/sssd/sssd.conf"
        mv_file "/etc/sssd/sssd.conf.bak" "/etc/sssd/sssd.conf"
    fi
    if [ -f "/etc/pki/tls/openssl.cnf.bak" ]; then
        rm_file_dir "/etc/pki/tls/openssl.cnf"
        mv_file "/etc/pki/tls/openssl.cnf.bak" "/etc/pki/tls/openssl.cnf"
    fi
    log_title "清理并结束LDAP服务搭建测试"

}

function ldap_run() {
    local param="$1"
    sv_name_rpm "${param}"
    ldap_bak
    ldap_build
    ldap_start
    ldap_test
    #ldap_clean
}

###        NETCONF服务       ###
function netconf_bak() {
    log_title "开始NETCONF服务搭建测试"
    local netconf_path="./netconf"
    if [ -f "/usr/local/lib64/libsysrepo.so.7" ]; then
        rm_file_dir "/usr/local/lib64/libsysrepo.so.7"
    fi
    if [ -f "/usr/local/lib64/libyang.so.2" ]; then
        rm_file_dir "/usr/local/lib64/libyang.so.2"
    fi
    if [ -f "/usr/local/lib64/libnetconf2.so.3" ]; then
        rm_file_dir "/usr/local/lib64/libnetconf2.so.3"
    fi
}

function netconf_build() {
    stop_se
    stop_fw

    cd ./netconf
    tar -zxvf netconf.tar.gz
    log_title "开始编译libyang"
    cd netconf/libyang
    if [ -d "build" ]; then
        rm_file_dir "build"
    fi
    mk_dir build
    cd build
    make clean
    cmake ..
    make
    make install

    log_title "开始编译libnetconf2"
    mv ../../libnetconf2/CMakeLists.txt ../../libnetconf2/CMakeLists.txt.bak
    cp ../../../CMakeLists_libnetconf2.txt ../../libnetconf2/CMakeLists.txt
    cd ../../libnetconf2
    if [ -d "build" ]; then
        rm_file_dir "build"
    fi
    mk_dir "build"
    cd build
    cmake ..
    make
    make install

    log_title "开始编译sysrepo"
    cd ../../sysrepo
    if [ -d "build" ]; then
        rm_file_dir "build"
    fi
    mk_dir "build"
    cd build
    cmake ..
    make
    make install

    log_title "开始编译uncrustify"
    cd ../../uncrustify
    if [ -d "build" ]; then
        rm_file_dir "build"
    fi
    mk_dir "build"
    cd build
    cmake ..
    make
    make install

    log_title "开始编译netopeer2"
    mv ../../netopeer2/CMakeLists.txt ../../netopeer2/CMakeLists.txt.bak
    cp ../../../CMakeLists_netopeer2.txt ../../netopeer2/CMakeLists.txt
    cd ../../netopeer2
    if [ -d "build" ]; then
        rm_file_dir "build"
    fi
    mk_dir "build"
    cd build
    cmake ..
    make
    ln -s /usr/local/lib64/libsysrepo.so.7 /lib64/libsysrepo.so.7
    ln -s /usr/local/lib64/libyang.so.2 /lib64/libyang.so.2
    ln -s /usr/local/lib64/libnetconf2.so.3 /lib64/libnetconf2.so.3
    make install
    if [ "$?" == 0 ]; then
        log_title "NETCONF搭建成功"
        log_ret "NETCONF搭建成功" "TPASS"
    else
        log_error "NETCONF搭建失败"
        log_ret "NETCONF搭建失败" "TFAIL"
    fi
    cd "${PWD_SV}"
}

function netconf_start() {
    netopeer2-server -d -v 2 &
}

function netconf_clean() {
    if [ -d "netconf/netconf" ]; then
        rm_file_dir "netconf/netconf"
    fi
    log_title "清理并结束NETCONF服务搭建测试"

}

function netconf_run() {
    local param="$1"
    sv_name_rpm "${param}"
    netconf_bak
    netconf_build
    netconf_start
}

###        RESTCONF服务       ###
function restconf_bak() {
    log_title "开始RESTCONF服务搭建测试"
    if [ -d "/usr/local/go" ]; then
        rm_file_dir "/usr/local/go"
    fi
}

function restconf_build() {
    stop_se
    stop_fw

    cd ./restconf
    local arch_type=$(arch)
    if [ "${arch_type}" == "x86_64" ]; then
        tar -C /usr/local -xzf go1.21.3.linux-amd64.tar.gz
    else
        tar -C /usr/local -xzf go1.20.3.linux-arm64.tar.gz
    fi
    export PATH=$PATH:/usr/local/go/bin
    go version
    tar -C /root -xvf go.tgz
    tar -C /root -xvf car.tgz
    cd /root/car
    go env -w GO111MODULE=off
    go build
    if [ "$?" != 0 ]; then
        log_ret "NETCONF环境编译失败" "TFAIL"
    fi
}

function restconf_start() {
    ./car >/dev/null 2>&1 &
    go run . >/dev/null 2>&1 &
    if [ "$?" != 0 ]; then
        log_ret "RESTCONF环境搭建失败" "TFAIL"
    else
        log_title "RESTCONF搭建成功"
    fi
    cd "${PWD_SV}"
}

function restconf_test() {
    killall lt-echo &>/dev/null
    sleep 3
    curl http://localhost:8080/restconf/data/car
    curl -XPOST http://localhost:8080/restconf/data/car: -d '{"speed":90}'
    curl -XPOST http://localhost:8080/restconf/data/car:add
    curl -XPOST http://localhost:8080/restconf/data/car:dec
    curl -XPOST http://localhost:8080/restconf/data/car:reset
    curl http://localhost:8080/restconf/data/car
    if [ "$?" == 0 ]; then
        log_title "RESTCONF搭建与测试成功"
        log_ret "RESTCONF搭建与测试成功" "TPASS"
    else
        log_error "RESTCONF搭建与测试失败"
        log_ret "RESTCONF搭建与测试失败" "TFAIL"
    fi
}

function restconf_clean() {
    if [ -d "/usr/local/go" ]; then
        rm_file_dir "/usr/local/go"
    fi
    if [ -d "/root/go" ]; then
        rm_file_dir "/root/go"
    fi
    if [ -d "/root/car" ]; then
        rm_file_dir "/root/car"
    fi
    local car_pid=$(ps -ef | grep ./car | awk -F' ' '{print $2}' | awk NR==1)
    kill -9 "${car_pid}"
    log_title "清理并结束RESTCONF服务搭建测试"
}

function restconf_run() {
    local param="$1"
    sv_name_rpm "${param}"
    restconf_bak
    restconf_build
    restconf_start
    restconf_test
}

###        FASTCGI服务       ###
function fastcgi_bak() {
    local fastcgi_path="./fastcgi"
    log_title "开始FASTCGI服务搭建测试"
    if [ -d "/usr/local/spawn-fcgi-master" ]; then
        rm_file_dir "/usr/local/spawn-fcgi-master"
    fi
    if [ -d "/usr/local/fcgi2-master" ]; then
        rm_file_dir "/usr/local/fcgi2-master"
    fi
    if [ -f "/etc/nginx/nginx.conf.bak" ]; then
        cp_file "${fastcgi_path}/nginx.conf" "/etc/nginx/nginx.conf"
    else
        mv_file "/etc/nginx/nginx.conf" "/etc/nginx/nginx.conf.bak"
        cp_file "${fastcgi_path}/nginx.conf" "/etc/nginx/nginx.conf"
    fi
}

function fastcgi_build() {
    stop_se
    stop_fw

    cd ./fastcgi
    unzip spawn-fcgi-master.zip
    unzip fcgi2-master.zip
    mv_file "spawn-fcgi-master" "/usr/local/"
    mv_file "fcgi2-master" "/usr/local/"
    cd /usr/local/spawn-fcgi-master
    ./autogen.sh
    ./configure
    make
    make install
    cd ../fcgi2-master
    ./autogen.sh
    ./configure
    make
    make install
    if [[ "$?" -ne 0 ]]; then
        log_error "编译失败，请下载autoconf|automake|libtool包！"
        exit 1
    fi
    cd examples
    spawn-fcgi -f ./echo -a 127.0.0.1 -p 8080
    if [ "$?" != 0 ]; then
        log_error "FASTCGI环境编译失败"
    fi
    cd "${PWD_SV}"
}

function fastcgi_start() {
    local packages=("nginx")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function fastcgi_test() {
    systemctl status nginx | grep ": active"
    if [ "$?" == "0" ]; then
        log_title "FASTCGI搭建与测试成功"
        log_ret "FASTCGI搭建与测试成功" "TPASS"
    else
        log_error "FASTCGI搭建与测试失败"
        log_ret "FASTCGI搭建与测试失败" "TFAIL"
    fi
}

function fastcgi_clean() {
    if [ -d "/usr/local/spawn-fcgi-master" ]; then
        rm_file_dir "/usr/local/spawn-fcgi-master"
    fi
    if [ -d "/usr/local/fcgi2-master" ]; then
        rm_file_dir "/usr/local/fcgi2-master"
    fi
    if [ -f "/etc/nginx/nginx.conf.bak" ]; then
        rm_file_dir "/etc/nginx/nginx.conf"
        mv_file "/etc/nginx/nginx.conf.bak" "/etc/nginx/nginx.conf"
    fi
    log_title "清理并结束FASTCGI服务搭建测试"
}

function fastcgi_run() {
    local param="$1"
    sv_name_rpm "${param}"
    fastcgi_bak
    fastcgi_build
    https_bak
    https_build
    https_start
    fastcgi_test
}

###        SNMP服务       ###
function snmp_bak() {
    local snmp_path="./snmp"
    log_title "开始SNMP服务搭建测试"
    if [ -f "/etc/snmp/snmpd.conf.bak" ]; then
        cp_file "${snmp_path}/snmpd.conf" "/etc/snmp/"
    else
        mv_file "/etc/snmp/snmpd.conf" "/etc/snmp/snmpd.conf.bak"
        cp_file "${snmp_path}/snmpd.conf" "/etc/snmp/"
    fi
}

function snmp_build() {
    stop_se
    stop_fw

    netstat -lntpu | grep snmpd | tee -a "${OUTPUT_FILE}"
    snmpd udp6:161
}

function snmp_start() {
    local packages=("snmpd")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function snmp_test() {
    log_title "SNMP-ipv4测试:"
    snmpwalk -v 2c -c public localhost if
    if [ "$?" == "0" ]; then
        log_title "SNMP搭建与测试成功"
        log_ret "SNMP搭建与测试成功" "TPASS"
    else
        log_error "SNMP搭建与测试失败"
        log_ret "SNMP搭建与测试失败" "TFAIL"
    fi
}

function snmp_clean() {
    if [ -f "/etc/snmp/snmpd.conf.bak" ]; then
        rm_file_dir "/etc/snmp/snmpd.conf"
        mv_file "/etc/snmp/snmpd.conf.bak" "/etc/snmp/snmpd.conf"
    fi
    local pid_snmp=($(ps aux | grep snmpd | awk -F' ' '{print $2}' | head -n 2))
    for i in "${pid_snmp[@]}"; do
        kill -9 "${i}"
    done
    log_title "清理并结束SNMP服务搭建测试"
}

function snmp_run() {
    local param="$1"
    sv_name_rpm "${param}"
    snmp_bak
    snmp_build
    snmp_start
    snmp_test
}

###        SMTP服务       ###
function smtp_bak() {
    local smtp_path="./smtp"
    log_title "开始SMTP服务搭建测试"
    if [ -f "/etc/hosts.bak" ]; then
        cp_file "/etc/hosts.bak" "/etc/hosts"
    else
        mv_file "/etc/hosts" "/etc/hosts.bak"
        cp_file "/etc/hosts.bak" "/etc/hosts"
    fi
    if [ -f "/etc/postfix/main.cf.bak" ]; then
        cp_file "${smtp_path}/main.cf" "/etc/postfix/"
    else
        mv_file "/etc/postfix/main.cf" "/etc/postfix/main.cf.bak"
        cp_file "${smtp_path}/main.cf" "/etc/postfix/"
    fi
    if [ -f "/etc/dovecot/dovecot.conf.bak" ]; then
        cp_file "${smtp_path}/dovecot.conf" "/etc/dovecot/"
    else
        mv_file "/etc/dovecot/dovecot.conf" "/etc/dovecot/dovecot.conf.bak"
        cp_file "${smtp_path}/dovecot.conf" "/etc/dovecot/"
    fi
    if [ -f "/etc/dovecot/conf.d/10-mail.conf.bak" ]; then
        cp_file "${smtp_path}/10-mail.conf" "/etc/dovecot/conf.d"
    else
        mv_file "/etc/dovecot/conf.d/10-mail.conf" "/etc/dovecot/conf.d/10-mail.conf.bak"
        cp_file "${smtp_path}/10-mail.conf" "/etc/dovecot/conf.d"
    fi
    if [ -f "/etc/dovecot/conf.d/10-ssl.conf.bak" ]; then
        cp_file "${smtp_path}/10-ssl.conf" "/etc/dovecot/conf.d"
    else
        mv_file "/etc/dovecot/conf.d/10-ssl.conf" "/etc/dovecot/conf.d/10-ssl.conf.bak"
        cp_file "${smtp_path}/10-ssl.conf" "/etc/dovecot/conf.d"
    fi
    if [ -f "/etc/dovecot/conf.d/10-auth.conf.bak" ]; then
        cp_file "${smtp_path}/10-auth.conf" "/etc/dovecot/conf.d"
    else
        mv_file "/etc/dovecot/conf.d/10-auth.conf" "/etc/dovecot/conf.d/10-auth.conf.bak"
        cp_file "${smtp_path}/10-auth.conf" "/etc/dovecot/conf.d"
    fi
}

function smtp_build() {
    stop_se
    stop_fw

    postconf -a
    if [ "$?" == "0" ]; then
        log_title "SMTP支持cyrus和dovecot功能"
    fi
    hostnamectl set-hostname mail.kysec.com
    hostname
    local ip_host=$(ifconfig | grep -E "broadcast" | awk "NR==1" | awk -F' ' '{print $2}')
    echo "${ip_host} mail.kysec.com kysec.com" >>/etc/hosts
    log_title "cat /etc/hosts"
    cat /etc/hosts | tee -a "${OUTPUT_FILE}"
    postfix check

    log_title "创建邮件账户jack与tom"
    useradd -g mail -s /sbin/nologin jack
    useradd -g mail -s /sbin/nologin tom
    set_pw jack
    set_pw tom
    chmod 777 /var/mail/jack
    chmod 777 /var/mail/tom

    log_title "postconf -n"
    postconf -n | tee -a "${OUTPUT_FILE}"
    log_title "dovecot -n"
    dovecot -n | tee -a "${OUTPUT_FILE}"

}

function smtp_start() {
    local packages=("postfix" "dovecot")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function smtp_test() {
    ss -ltnp | grep dovecot
    if [ "$?" == "0" ]; then
        ss -ltnp | grep dovecot | tee -a "${OUTPUT_FILE}"
        log_title "SMTP搭建与测试成功"
        log_ret "SMTP搭建与测试成功" "TPASS"
    else
        log_error "SMTP搭建与测试失败"
        log_ret "SMTP搭建与测试失败" "TFAIL"
    fi
}

function smtp_clean() {
    if [ -f "/etc/postfix/main.cf.bak" ]; then
        rm_file_dir "/etc/postfix/main.cf"
        mv_file "/etc/postfix/main.cf.bak" "/etc/postfix/main.cf"
    fi
    if [ -f "/etc/dovecot/dovecot.conf.bak" ]; then
        rm_file_dir "/etc/dovecot/dovecot.conf"
        mv_file "/etc/dovecot/dovecot.conf.bak" "/etc/dovecot/dovecot.conf"
    fi
    if [ -f "/etc/dovecot/conf.d/10-mail.conf.bak" ]; then
        rm_file_dir "/etc/dovecot/conf.d/10-mail.conf"
        mv_file "/etc/dovecot/conf.d/10-mail.conf.bak" "/etc/dovecot/conf.d/10-mail.conf"
    fi
    if [ -f "/etc/dovecot/conf.d/10-ssl.conf.bak" ]; then
        rm_file_dir "/etc/dovecot/conf.d/10-ssl.conf"
        mv_file "/etc/dovecot/conf.d/10-ssl.conf.bak" "/etc/dovecot/conf.d/10-ssl.conf"
    fi
    if [ -f "/etc/dovecot/conf.d/10-auth.conf.bak" ]; then
        rm_file_dir "/etc/dovecot/conf.d/10-auth.conf"
        mv_file "/etc/dovecot/conf.d/10-auth.conf.bak" "/etc/dovecot/conf.d/10-auth.conf"
    fi
    log_title "清理并结束SMTP服务搭建测试"
}

function smtp_run() {
    local param="$1"
    sv_name_rpm "${param}"
    smtp_bak
    smtp_build
    smtp_start
    smtp_test
}

###        NTP服务       ###
function ntp_bak() {
    local ntp_path="./ntp"
    log_title "开始NTP服务搭建测试"
    if [ -f "/etc/ntp.conf.bak" ]; then
        cp_file "${ntp_path}/ntp.conf" "/etc/ntp.conf"
    else
        mv_file "/etc/ntp.conf" "/etc/ntp.conf.bak"
        cp_file "${ntp_path}/ntp.conf" "/etc/ntp.conf"
    fi

}

function ntp_build() {
    stop_se
    stop_fw

    local ip_local=$(ifconfig | grep -E "broadcast" | awk "NR==1" | awk -F' ' '{print $2}')
    local ip_1=$(echo ${ip_local} | awk -F'.' '{print $1}')
    local ip_2=$(echo ${ip_local} | awk -F'.' '{print $2}')
    local ip_3=$(echo ${ip_local} | awk -F'.' '{print $3}')
    echo "restrict ${ip_1}.${ip_2}.${ip_3}.0 mask 255.255.0.0 nomodify notrap" >>/etc/ntp.conf
}

function ntp_start() {
    local packages=("ntpd")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function ntp_test() {
    systemctl status ntpd | grep ": active"
    if [ "$?" == "0" ]; then
        log_title "NTP搭建与测试成功"
        log_ret "NTP搭建与测试成功" "TPASS"
    else
        log_error "NTP搭建与测试失败"
        log_ret "NTP搭建与测试失败" "TFAIL"
    fi
}

function ntp_clean() {
    if [ -f "/etc/ntp.conf.bak" ]; then
        rm_file_dir "/etc/ntp.conf"
        mv_file "/etc/ntp.conf.bak" "/etc/ntp.conf"
    fi

    log_title "清理并结束NTP服务搭建测试"
}

function ntp_run() {
    local param="$1"
    sv_name_rpm "${param}"
    ntp_bak
    ntp_build
    ntp_start
    ntp_test
}

###        RBAC服务       ###
function rbac_bak() {
    log_title "开始RBAC服务搭建测试"
}

function rbac_build() {
    stop_se
    stop_fw

    cat /etc/passwd | grep "sysadm"
    cat /etc/passwd | grep "secadm"
    cat /etc/passwd | grep "audadm"

    set_pw sysadm
    set_pw secadm
    set_pw audadm

    ssh_auto "sysadm" "localhost" "kylin.2023"
    ssh_auto "audadm" "localhost" "kylin.2023"
    ssh_auto "secadm" "localhost" "kylin.2023"
}

function rbac_start() {
    local packages=("sshd")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function rbac_test() {
    ssh_auto_cmd "secadm" "localhost" "sudo semanage user -l|grep sysadm_r"
    ssh_auto_cmd "secadm" "localhost" "sudo semanage user -l|grep secadm_r"
    ssh_auto_cmd "secadm" "localhost" "sudo semanage user -l|grep auditadm_r"
    if [ "$?" == "0" ]; then
        log_title "RBAC搭建与测试成功"
        log_ret "RBAC搭建与测试成功" "TPASS"
    else
        log_error "RBAC搭建与测试失败"
        log_ret "RBAC搭建与测试失败" "TFAIL"
    fi
}

function rbac_clean() {
    log_title "清理并结束RBAC服务搭建测试"
}

function rbac_run() {
    local param="$1"
    sv_name_rpm "${param}"
    rbac_bak
    rbac_build
    rbac_start
    rbac_test
}

###        RPC服务       ###
function rpc_bak() {
    log_title "开始RPC服务搭建测试"
}

function rpc_build() {
    stop_se
    stop_fw
}

function rpc_start() {
    local packages=("rpcbind")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function rpc_test() {
    log_title "RPC服务的注册状况如下："
    rpcinfo localhost
    if [ "$?" == "0" ]; then
        rpcinfo localhost | tee -a "${OUTPUT_FILE}" >/dev/null
        log_title "RPC搭建与测试成功"
        log_ret "RPC搭建与测试成功" "TPASS"
    else
        log_error "RPC搭建与测试失败"
        log_ret "RPC搭建与测试失败" "TFAIL"
    fi
}

function rpc_clean() {
    log_title "清理并结束RPC服务搭建测试"
}

function rpc_run() {
    local param="$1"
    sv_name_rpm "${param}"
    rpc_bak
    rpc_build
    rpc_start
    rpc_test
}

###        RSYNC服务       ###
function rsync_bak() {
    if [ -f "/tmp/rsync_test.txt" ]; then
        rm_file_dir "/tmp/rsync_test.txt"
    fi
    log_title "开始RSYNC服务搭建测试"
}

function rsync_build() {
    stop_se
    stop_fw

    useradd "rsync_user"
    set_pw "rsync_user"
    ssh_auto "rsync_user" "localhost" "kylin.2023"
}

function rsync_test() {
    local rsync_file="/tmp/rsync_test.txt"
    local rsync_user_file="/home/rsync_user/rsync_test.txt"
    echo "This is rsync test" >"${rsync_file}"
    rsync -av "${rsync_file}" "rsync_user"@"localhost":"${rsync_user_file}"
    ls_file_dir "${rsync_user_file}"
    if [ "$?" == "0" ]; then
        log_title "RSYNC搭建与测试成功"
        log_ret "RSYNC搭建与测试成功" "TPASS"
    else
        log_error "RSYNC搭建与测试失败"
        log_ret "RSYNC搭建与测试失败" "TFAIL"
    fi
}

function rsync_clean() {
    if [ -f "/tmp/rsync_test.txt" ]; then
        rm_file_dir "/tmp/rsync_test.txt"
    fi
    del_user "rsync_user"
    log_title "清理并结束RSYNC服务搭建测试"
}

function rsync_run() {
    local param="$1"
    sv_name_rpm "${param}"
    rsync_bak
    rsync_build
    rsync_test
}

###        SSH服务       ###
function ssh_bak() {
    log_title "开始SSH服务搭建测试"
}

function ssh_build() {
    stop_se
    stop_fw

    useradd "ssh_user"
    set_pw "ssh_user"
    ssh_auto "ssh_user" "localhost" "kylin.2023"
}

function ssh_start() {
    local packages=("sshd")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function ssh_test() {
    ssh_auto_cmd "ssh_user" "localhost" "pwd;exit"
    if [ "$?" == "0" ]; then
        log_title "SSH搭建与测试成功"
        log_ret "SSH搭建与测试成功" "TPASS"
    else
        log_error "SSH搭建与测试失败"
        log_ret "SSH搭建与测试失败" "TFAIL"
    fi
}

function ssh_clean() {
    del_user "ssh_user"
    log_title "清理并结束SSH服务搭建测试"
}

function ssh_run() {
    local param="$1"
    sv_name_rpm "${param}"
    ssh_bak
    ssh_build
    ssh_start
    ssh_test
}

###        STRONGSWAN服务       ###
function strongswan_bak() {
    log_title "开始STRONGSWAN服务搭建测试"
    local strongswan_path="./strongswan"
    if [ -f "/etc/strongswan/ipsec.conf.bak" ]; then
        cp_file "${strongswan_path}/server/ipsec.conf" "/etc/strongswan/ipsec.conf"
    else
        mv_file "/etc/strongswan/ipsec.conf" "/etc/strongswan/ipsec.conf.bak"
        cp_file "${strongswan_path}/server/ipsec.conf" "/etc/strongswan/ipsec.conf"
    fi
    if [ -f "/etc/strongswan/ipsec.secrets.bak" ]; then
        cp_file "${strongswan_path}/server/ipsec.secrets" "/etc/strongswan/ipsec.secrets"
    else
        mv_file "/etc/strongswan/ipsec.secrets" "/etc/strongswan/ipsec.secrets.bak"
        cp_file "${strongswan_path}/server/ipsec.secrets" "/etc/strongswan/ipsec.secrets"
    fi
}

function strongswan_build() {
    stop_se
    stop_fw
    cd /etc/strongswan
    local ser_ip="$1"
    local cli_ip="$2"
    local ip_1=$(echo ${ser_ip} | awk -F'.' '{print $1}')
    local ip_2=$(echo ${ser_ip} | awk -F'.' '{print $2}')
    local ip_3=$(echo ${ser_ip} | awk -F'.' '{print $3}')
    local netmask=$(ip addr show | grep ${ser_ip} | awk '{print $2}' | cut -d'/' -f2)
    echo -e "\tleftid=${ser_ip}" >>"/etc/strongswan/ipsec.conf"
    echo -e "\tleftsubnet=${ip_1}.${ip_2}.${ip_3}.0/${netmask}" >>"/etc/strongswan/ipsec.conf"
    echo -e "\tright=${cli_ip}" >>"/etc/strongswan/ipsec.conf"
    echo -e "\trightsubnet=${ip_1}.${ip_2}.${ip_3}.0/${netmask}" >>"/etc/strongswan/ipsec.conf"
    echo -e "\tauto=start" >>"/etc/strongswan/ipsec.conf"
    echo "${ser_ip} ${cli_ip} : PSK qcloud123" >"/etc/strongswan/ipsec.secrets"
    sed -i "s/net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/g" "/etc/sysctl.conf"
    sysctl -p | tee -a "${OUTPUT_FILE}"
}

function strongswan_start() {
    local packages=("strongswan")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
}

function strongswan_client() {
    stop_se
    stop_fw
    local strongswan_path="./strongswan"
    if [ -f "/etc/strongswan/ipsec.conf.bak" ]; then
        cp_file "${strongswan_path}/client/ipsec.conf" "/etc/strongswan/ipsec.conf"
    else
        mv_file "/etc/strongswan/ipsec.conf" "/etc/strongswan/ipsec.conf.bak"
        cp_file "${strongswan_path}/client/ipsec.conf" "/etc/strongswan/ipsec.conf"
    fi
    if [ -f "/etc/strongswan/ipsec.secrets.bak" ]; then
        cp_file "${strongswan_path}/client/ipsec.secrets" "/etc/strongswan/ipsec.secrets"
    else
        mv_file "/etc/strongswan/ipsec.secrets" "/etc/strongswan/ipsec.secrets.bak"
        cp_file "${strongswan_path}/client/ipsec.secrets" "/etc/strongswan/ipsec.secrets"
    fi
    cd /etc/strongswan
    local ser_ip="$1"
    local cli_ip="$2"
    local ip_1=$(echo ${ser_ip} | awk -F'.' '{print $1}')
    local ip_2=$(echo ${ser_ip} | awk -F'.' '{print $2}')
    local ip_3=$(echo ${ser_ip} | awk -F'.' '{print $3}')
    local netmask=$(ip addr show | grep ${cli_ip} | awk '{print $2}' | cut -d'/' -f2)
    echo -e "\tleftid=${cli_ip}" >>"/etc/strongswan/ipsec.conf"
    echo -e "\tleftsubnet=${ip_1}.${ip_2}.${ip_3}.0/${netmask}" >>"/etc/strongswan/ipsec.conf"
    echo -e "\tright=${ser_ip}" >>"/etc/strongswan/ipsec.conf"
    echo -e "\trightsubnet=${ip_1}.${ip_2}.${ip_3}.0/${netmask}" >>"/etc/strongswan/ipsec.conf"
    echo -e "\tauto=start" >>"/etc/strongswan/ipsec.conf"
    echo "${cli_ip} ${ser_ip} : PSK qcloud123" >"/etc/strongswan/ipsec.secrets"
    sed -i "s/net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/g" "/etc/sysctl.conf"
    sysctl -p | tee -a "${OUTPUT_FILE}"

    local packages=("strongswan")
    for i in "${packages[@]}"; do
        st_sv "${i}"
    done
    systemctl status strongswan | grep ": active"
    if [ "$?" == "0" ]; then
        log_title "STRONGSWAN搭建成功"
        log_ret "STRONGSWAN搭建成功" "TPASS"
    else
        log_error "STRONGSWAN搭建与测试失败"
        log_ret "STRONGSWAN搭建与测试失败" "TFAIL"
    fi
}

function strongswan_test() {
    log_title "strongswan服务状态如下："
    systemctl status strongswan | grep ": active"
    log_title "启动myvpn："
    local vpn_name="myvpn"
    sudo strongswan up ${vpn_name} | grep "success" &>/dev/null
    if [ "$?" == "0" ]; then
        strongswan up myvpn | tee -a "${OUTPUT_FILE}"
        log_title "STRONGSWAN搭建成功"
        log_ret "STRONGSWAN搭建成功" "TPASS"
    else
        log_error "STRONGSWAN搭建与测试失败"
        log_ret "STRONGSWAN搭建与测试失败" "TFAIL"
    fi
}

function strongswan_clean() {
    if [ -f "/etc/strongswan/ipsec.conf.bak" ]; then
        rm_file_dir "/etc/strongswan/ipsec.conf"
        mv_file "/etc/strongswan/ipsec.conf.bak" "/etc/strongswan/ipsec.conf"
    fi
    if [ -f "/etc/strongswan/ipsec.secrets.bak" ]; then
        rm_file_dir "/etc/strongswan/ipsec.secrets"
        mv_file "/etc/strongswan/ipsec.secrets.bak" "/etc/strongswan/ipsec.secrets"
    fi
    strongswan down myvpn
    log_title "清理并结束STRONGSWAN服务搭建测试"
}

function strongswan_run() {
    local param="$1"
    local ser_ip="$2"
    local cli_ip="$3"
    sv_name_rpm "${param}"
    strongswan_bak
    strongswan_build "${ser_ip}" "${cli_ip}"
    strongswan_start
    strongswan_test
}

###        CEPH服务       ###
function ceph_bak() {
    log_title "开始CEPH服务搭建测试"
    local ceph_path="./ceph"
    if [ -f "/etc/hosts.bak" ]; then
        cp_file "${ceph_path}/hosts" "/etc/hosts"
    else
        mv_file "/etc/hosts" "/etc/hosts.bak"
        cp_file "${ceph_path}/hosts" "/etc/hosts"
    fi
    if [ -f "/etc/kylin-release.bak" ]; then
        cp_file "${ceph_path}/kylin-release" "/etc/kylin-release"
    else
        mv_file "/etc/kylin-release" "/etc/kylin-release.bak"
        cp_file "${ceph_path}/kylin-release" "/etc/kylin-release"
    fi
    if [ -f "/root/tmp" ]; then
        rm_file_dir "/root/tmp"
    fi
}

function ceph_build() {
    stop_fw
    local ceph_path="./ceph"
    local ser_ip="$1"
    local cli_ip="$2"
    local dev_name="$3"
    local selinux=$(getenforce)
    if [ "${selinux}" = "Enforcing" ]; then
        stop_se
        log_error "请关闭安全！"
    else
        if [ "$#" -ne 3 ]; then
            log_error "你必须输入server ip和client ip"
        fi
    fi
    hostnamectl --static set-hostname ceph-100
    sed -i "s/10.90.21.666/${ser_ip}/g" "/etc/hosts"
    sed -i "s/10.90.21.888/${cli_ip}/g" "/etc/hosts"
    cat /etc/hosts | tee -a "${OUTPUT_FILE}"
    # 密钥对不存在则创建密钥
    #[ ! -f "$rsapub_file_ssha" ] && ssh-keygen -t rsa -N '' -f ${rsapath_ssha}/id_rsa -q
    ssh_auto "root" "${ser_ip}" "kylin.2023"
    ssh_auto "root" "${cli_ip}" "kylin.2023"
    mk_dir "/root/tmp"
    cp_file "${ceph_path}/ceph-deploy-2.0.1.tar.gz" "/root/tmp"
    pip install --no-index --find-links=/root/tmp ceph-deploy
    cp_file "${ceph_path}/__init__.py" "/lib/python2.7/site-packages/ceph_deploy/hosts/"

    local service_pwd=$(pwd)
    log_title "开始部署MON服务"
    cd "/etc/ceph"
    log_title "创建集群"
    ceph-deploy new ceph-100
    cat "/etc/ceph/ceph.conf"
    if [ ! -f "/etc/ceph/ceph.conf.bak" ]; then
        mv_file "/etc/ceph/ceph.conf" "/etc/ceph/ceph.conf.bak"
    fi
    sed -i "s/mon_initial_members = ceph-100/mon_initial_members = ceph-100,ceph-101/g" "/etc/ceph/ceph.conf"
    sed -i "s/mon_host = ${ser_ip}/mon_host = ${ser_ip},${cli_ip}/g" "/etc/ceph/ceph.conf"
    local ip_1=$(echo ${ser_ip} | awk -F'.' '{print $1}')
    local ip_2=$(echo ${ser_ip} | awk -F'.' '{print $2}')
    local ip_3=$(echo ${ser_ip} | awk -F'.' '{print $3}')
    local netmask=$(ip addr show | grep ${ser_ip} | awk '{print $2}' | cut -d'/' -f2)
    #此处网段是10.90.21.0，有些可能是10.80.0.0，还需完善
    echo "public_network = ${ip_1}.${ip_2}.${ip_3}.0/${netmask}" >>"/etc/ceph/ceph.conf"
    echo "cluster_network = ${ip_1}.${ip_2}.${ip_3}.0/${netmask}" >>"/etc/ceph/ceph.conf"
    echo "[mon]" >>"/etc/ceph/ceph.conf"
    echo "mon_allow_pool_delete = true" >>"/etc/ceph/ceph.conf"
    cat "/etc/ceph/ceph.conf"
    scp "/etc/ceph/ceph.conf" "${cli_ip}":"/etc/ceph/"

    log_title "初始化MON服务"
    #ceph-deploy mon create-initial
    ceph-deploy --overwrite-conf mon create-initial
    ls
    log_title "将ceph.client.admin.keyring拷贝到各个节点上"
    ceph-deploy --overwrite-conf admin ceph-100 ceph-101
    log_title "校验是否配置成功"
    ceph -s | grep "mon" | tee -a "${OUTPUT_FILE}"

    log_title "开始部署MGR服务"
    /usr/bin/ceph-deploy mgr create ceph-100 ceph-101
    log_title "校验是否配置成功"
    ceph -s | grep "mgr" | tee -a "${OUTPUT_FILE}"
    log_title "列出硬盘信息"
    ceph-deploy disk list ceph-100
    log_title "擦除硬盘数据"
    ceph-deploy disk zap ceph-100 "${dev_name}"
    log_title "添加OSD"
    ceph-deploy osd create ceph-100 --data "${dev_name}"

    log_title "创建块存储RBD"
    ceph osd pool create myrbd 128 128
    ceph osd pool application enable myrbd rbd
    ceph osd pool set myrbd min_size 1
    rbd pool init -p myrbd
    rbd create myimg -s 1G --image-feature layering -p myrbd
    rbd ls -p myrbd
    rbd info myimg -p myrbd
    rbd map myimg -p myrbd
    rbd showmapperd
    lsblk | tee -a "${OUTPUT_FILE}"

    log_title "文件系统MDS"
    ceph-deploy mds create ceph-100
    ceph osd pool create cephfs-1 4
    ceph osd pool create cephfs-2 4
    ceph fs new myfs1 cephfs-1 cephfs-2
    ceph fs ls
    ceph osd pool set cephfs-1 min_size 1
    mk_dir "/mnt/cephfs"
    ceph-fuse -m ${ser_ip}:6789 "/mnt/cephfs"
    local key=$(cat /etc/ceph/ceph.client.admin.keyring | tail -n 1 | awk -F " = " '{print $2}')
    mount -t ceph ${ser_ip}:/ /mnt/cephfs -o name=admin,secret="${key}"
    df -Th | tee -a "${OUTPUT_FILE}"
}

function ceph_client() {
    local ceph_path="./ceph"
    if [ -f "/etc/hosts.bak" ]; then
        cp_file "${ceph_path}/hosts" "/etc/hosts"
    else
        mv_file "/etc/hosts" "/etc/hosts.bak"
        cp_file "${ceph_path}/hosts" "/etc/hosts"
    fi
    if [ -f "/etc/kylin-release.bak" ]; then
        cp_file "${ceph_path}/kylin-release" "/etc/kylin-release"
    else
        mv_file "/etc/kylin-release" "/etc/kylin-release.bak"
        cp_file "${ceph_path}/kylin-release" "/etc/kylin-release"
    fi

    stop_fw

    local ser_ip="$1"
    local cli_ip="$2"
    local selinux=$(getenforce)
    if [ "${selinux}" = "Enforcing" ]; then
        stop_se
        log_error "请关闭安全！"
    else
        if [ "$#" -ne 2 ]; then
            log_error "你必须输入server ip和client ip"
        fi
    fi
    hostnamectl --static set-hostname ceph-101
    sed -i "s/10.90.21.666/${ser_ip}/g" "/etc/hosts"
    sed -i "s/10.90.21.888/${cli_ip}/g" "/etc/hosts"
    cat /etc/hosts | tee -a "${OUTPUT_FILE}"
    ssh_auto "root" "${ser_ip}" "kylin.2023"
}

function ceph_test() {
    log_title "对象存储RGW"
    ceph-deploy rgw create ceph-100
    ceph osd pool ls
    if [ "$?" == "0" ]; then
        ceph osd pool ls | tee -a "${OUTPUT_FILE}"
        log_title "CEPH搭建成功"
        log_ret "CEPH搭建成功" "TPASS"
    else
        log_error "CEPH搭建与测试失败"
        log_ret "CEPH搭建与测试失败" "TFAIL"
    fi
}

function ceph_clean() {
    if [ -f "/etc/hosts.bak" ]; then
        rm_file_dir "/etc/hosts"
        mv_file "/etc/hosts.bak" "/etc/hosts"
    fi
    if [ -f "/etc/ceph/ceph.conf.bak" ]; then
        rm_file_dir "/etc/ceph/ceph.conf"
        mv_file "/etc/ceph/ceph.conf.bak" "/etc/ceph/ceph.conf"
    fi
    if [ -f "/etc/kylin-release.bak" ]; then
        rm_file_dir "/etc/kylin-release"
        mv_file "/etc/kylin-release.bak" "/etc/kylin-release"
    fi
    if [ -f "/root/tmp" ]; then
        rm_file_dir "/root/tmp"
    fi
    rm_file_dir "ceph.conf"
    rm_file_dir "ceph.mon.keyring"
    rm_file_dir "ceph-deploy-ceph.log"
    log_title "清理并结束CEPH服务搭建测试"
}

function ceph_run() {
    local param="$1"
    local ser_ip="$2"
    local cli_ip="$3"
    local dev_name="$4"
    sv_name_rpm "${param}"
    ceph_bak
    ceph_build "${ser_ip}" "${cli_ip}" "${dev_name}"
    ceph_test
}

###        HADOOP服务       ###
function hadoop_bak() {
    log_title "开始HADOOP服务搭建测试"
    local had_pwd=$(pwd)
    local hadoop_path="${had_pwd}/hadoop"
    if [ -f "/etc/sudoers.bak" ]; then
        cp_file "${hadoop_path}/sudoers" "/etc/sudoers"
    else
        mv_file "/etc/sudoers" "/etc/sudoers.bak"
        cp_file "${hadoop_path}/sudoers" "/etc/sudoers"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/etc/hadoop/core-site.xml.bak" ]; then
        cp_file "${hadoop_path}/core-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/"
    else
        mv_file "/opt/module/hadoop-3.3.4/etc/hadoop/core-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/core-site.xml.bak"
        cp_file "${hadoop_path}/core-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/etc/hadoop/hdfs-site.xml.bak" ]; then
        cp_file "${hadoop_path}/hdfs-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/"
    else
        mv_file "/opt/module/hadoop-3.3.4/etc/hadoop/hdfs-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/hdfs-site.xml.bak"
        cp_file "${hadoop_path}/hdfs-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/etc/hadoop/yarn-site.xml.bak" ]; then
        cp_file "${hadoop_path}/yarn-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/"
    else
        mv_file "/opt/module/hadoop-3.3.4/etc/hadoop/yarn-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/yarn-site.xml.bak"
        cp_file "${hadoop_path}/yarn-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/etc/hadoop/mapred-site.xml.bak" ]; then
        cp_file "${hadoop_path}/mapred-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/"
    else
        mv_file "/opt/module/hadoop-3.3.4/etc/hadoop/mapred-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/mapred-site.xml.bak"
        cp_file "${hadoop_path}/mapred-site.xml" "/opt/module/hadoop-3.3.4/etc/hadoop/"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/etc/hadoop/workers.bak" ]; then
        cp_file "${hadoop_path}/workers" "/opt/module/hadoop-3.3.4/etc/hadoop/"
    else
        mv_file "/opt/module/hadoop-3.3.4/etc/hadoop/workers" "/opt/module/hadoop-3.3.4/etc/hadoop/workers.bak"
        cp_file "${hadoop_path}/workers" "/opt/module/hadoop-3.3.4/etc/hadoop/"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/sbin/stop-yarn.sh.bak" ]; then
        cp_file "${hadoop_path}/stop-yarn.sh" "/opt/module/hadoop-3.3.4/sbin/stop-yarn.sh"
    else
        mv_file "/opt/module/hadoop-3.3.4/sbin/stop-yarn.sh" "/opt/module/hadoop-3.3.4/sbin/stop-yarn.sh.bak"
        cp_file "${hadoop_path}/stop-yarn.sh" "/opt/module/hadoop-3.3.4/sbin/stop-yarn.sh"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/sbin/start-yarn.sh.bak" ]; then
        cp_file "${hadoop_path}/start-yarn.sh" "/opt/module/hadoop-3.3.4/sbin/start-yarn.sh"
    else
        mv_file "/opt/module/hadoop-3.3.4/sbin/start-yarn.sh" "/opt/module/hadoop-3.3.4/sbin/start-yarn.sh.bak"
        cp_file "${hadoop_path}/start-yarn.sh" "/opt/module/hadoop-3.3.4/sbin/start-yarn.sh"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/sbin/stop-dfs.sh.bak" ]; then
        cp_file "${hadoop_path}/stop-dfs.sh" "/opt/module/hadoop-3.3.4/sbin/stop-dfs.sh"
    else
        mv_file "/opt/module/hadoop-3.3.4/sbin/stop-dfs.sh" "/opt/module/hadoop-3.3.4/sbin/stop-dfs.sh.bak"
        cp_file "${hadoop_path}/stop-dfs.sh" "/opt/module/hadoop-3.3.4/sbin/stop-dfs.sh"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/sbin/start-dfs.sh.bak" ]; then
        cp_file "${hadoop_path}/start-dfs.sh" "/opt/module/hadoop-3.3.4/sbin/start-dfs.sh"
    else
        mv_file "/opt/module/hadoop-3.3.4/sbin/start-dfs.sh" "/opt/module/hadoop-3.3.4/sbin/start-dfs.sh.bak"
        cp_file "${hadoop_path}/start-dfs.sh" "/opt/module/hadoop-3.3.4/sbin/start-dfs.sh"
    fi
}

function hadoop_build() {
    stop_fw
    stop_se

    local hadoop_tar="http://10.0.0.66/opensource/hadoop.git"
    local hadoop_path="./hadoop"
    local ser_ip="$1"
    local cli_ip="$2"
    hostnamectl set-hostname master
    mk_dir "/opt/module"
    mk_dir "/opt/software"
    ssh_auto "root" "${ser_ip}" "kylin.2023"
    ssh_auto "root" "${cli_ip}" "kylin.2023"
    if [ -f "/etc/hosts.bak" ]; then
        cp_file "${hadoop_path}/hosts" "/etc/hosts"
    else
        mv_file "/etc/hosts" "/etc/hosts.bak"
        cp_file "${hadoop_path}/hosts" "/etc/hosts"
    fi
    if [ -f "/etc/hostsname.bak" ]; then
        cp_file "${hadoop_path}/hostsname" "/etc/hostsname"
    else
        mv_file "/etc/hostsname" "/etc/hostsname.bak"
        cp_file "${hadoop_path}/hostsname" "/etc/hostsname"
    fi
    sed -i "s/10.90.21.666/${ser_ip}/g" "/etc/hosts"
    sed -i "s/10.90.21.888/${cli_ip}/g" "/etc/hosts"
    cd "${hadoop_path}"
    git clone "${hadoop_tar}"
    cd -
    tar -zxvf "${hadoop_path}/hadoop-3.3.4.tar.gz" -C "/opt/module/" >/dev/null
    cp_file "${hadoop_path}/my_env.sh" "/etc/profile.d/"
    source /etc/profile
    log_title "启动集群需要初始化"
    rm_file_dir "/root/hadoopdata/"
    rm_file_dir /tmp/hadoop-root-namenode.pid
    /opt/module/hadoop-3.3.4/bin/hdfs namenode -format
    ls_file_dir "/opt/module/hadoop-3.3.4/logs"
    log_title "启动HDFS"
    hadoop_bak
    chmod -R 755 /opt/module/hadoop-3.3.4/sbin
    /opt/module/hadoop-3.3.4/sbin/start-all.sh
}

function hadoop_client() {
    stop_fw
    stop_se

    local hadoop_path="./hadoop"
    local ser_ip="$1"
    local cli_ip="$2"
    hostnamectl set-hostname slaver01
    mk_dir "/opt/module"
    mk_dir "/opt/software"
    ssh_auto "root" "${ser_ip}" "kylin.2023"
    ssh_auto "root" "${cli_ip}" "kylin.2023"
    if [ -f "/etc/hosts.bak" ]; then
        cp_file "${hadoop_path}/hosts" "/etc/hosts"
    else
        mv_file "/etc/hosts" "/etc/hosts.bak"
        cp_file "${hadoop_path}/hosts" "/etc/hosts"
    fi
    if [ -f "/etc/hostsname.bak" ]; then
        cp_file "${hadoop_path}/hostsname" "/etc/hostsname"
    else
        mv_file "/etc/hostsname" "/etc/hostsname.bak"
        cp_file "${hadoop_path}/hostsname" "/etc/hostsname"
    fi
    sed -i "s/master/slaver01/g" "/etc/hostsname"
    sed -i "s/10.90.21.666/${ser_ip}/g" "/etc/hosts"
    sed -i "s/10.90.21.888/${cli_ip}/g" "/etc/hosts"
    tar -zxvf "${hadoop_path}/hadoop-3.3.4.tar.gz" -C "/opt/module/" >/dev/null
    cp_file "${hadoop_path}/my_env.sh" "/etc/profile.d/"
    source /etc/profile
    java
    hadoop
    log_title "启动集群需要初始化"
    rm_file_dir "/root/hadoopdata/"
    rm_file_dir /tmp/hadoop-root-namenode.pid
    /opt/module/hadoop-3.3.4/bin/hdfs namenode -format
    ls_file_dir "/opt/module/hadoop-3.3.4/logs"
    log_title "启动YARN"
    hadoop_bak
    chmod -R 755 /opt/module/hadoop-3.3.4/sbin
    /opt/module/hadoop-3.3.4/sbin/start-all.sh
    jps
    if [ "$?" == "0" ]; then
        log_title "HADOOP客户端搭建成功"
        log_ret "HADOOP客户端搭建成功" "TPASS"
    else
        log_error "HADOOP客户端搭建失败"
        log_ret "HADOOP客户端搭建失败" "TFAIL"
    fi
}

function hadoop_test() {
    log_title "jps查看有无NameNode"
    jps | grep " NameNode"
    if [ "$?" == "0" ]; then
        log_title "jps信息："
        jps
        log_title "HADOOP服务端搭建成功"
        log_ret "HADOOP服务端搭建成功" "TPASS"
    else
        rm_file_dir "/root/hadoopdata/"
        rm_file_dir /tmp/hadoop-root-namenode.pid
        source /etc/profile
        /opt/module/hadoop-3.3.4/bin/hdfs namenode -format
        chmod -R 755 /opt/module/hadoop-3.3.4/sbin
        /opt/module/hadoop-3.3.4/sbin/start-all.sh
        jps | grep " NameNode"
        if [ "$?" != "0" ]; then
            log_error "HADOOP服务端搭建失败"
            log_ret "HADOOP服务端搭建失败" "TFAIL"
        else
            log_title "HADOOP服务端搭建成功"
            log_ret "HADOOP服务端搭建成功" "TPASS"
        fi
    fi
}

function hadoop_clean() {
    if [ -f "/etc/sudoers.bak" ]; then
        rm_file_dir "/etc/sudoers"
        mv_file "/etc/sudoers.bak" "/etc/sudoers"
    fi
    if [ -f "/etc/hosts.bak" ]; then
        rm_file_dir "/etc/hosts"
        mv_file "/etc/hosts.bak" "/etc/hosts"
    fi
    if [ -f "/etc/hostsname.bak" ]; then
        rm_file_dir "/etc/hostsname"
        mv_file "/etc/hostsname.bak" "/etc/hostsname"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/etc/hadoop/core-site.xml.bak" ]; then
        rm_file_dir "/opt/module/hadoop-3.3.4/etc/hadoop/core-site.xml"
        mv_file "/opt/module/hadoop-3.3.4/etc/hadoop/core-site.xml.bak" "/opt/module/hadoop-3.3.4/etc/hadoop/core-site.xml"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/etc/hadoop/hdfs-site.xml.bak" ]; then
        rm_file_dir "/opt/module/hadoop-3.3.4/etc/hadoop/hdfs-site.xml"
        mv_file "/opt/module/hadoop-3.3.4/etc/hadoop/hdfs-site.xml.bak" "/opt/module/hadoop-3.3.4/etc/hadoop/hdfs-site.xml"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/etc/hadoop/yarn-site.xml.bak" ]; then
        rm_file_dir "/opt/module/hadoop-3.3.4/etc/hadoop/yarn-site.xml"
        mv_file "/opt/module/hadoop-3.3.4/etc/hadoop/yarn-site.xml.bak" "/opt/module/hadoop-3.3.4/etc/hadoop/yarn-site.xml"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/etc/hadoop/mapred-site.xml.bak" ]; then
        rm_file_dir "/opt/module/hadoop-3.3.4/etc/hadoop/mapred-site.xml"
        mv_file "/opt/module/hadoop-3.3.4/etc/hadoop/mapred-site.xml.bak" "/opt/module/hadoop-3.3.4/etc/hadoop/mapred-site.xml"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/etc/hadoop/workers.bak" ]; then
        rm_file_dir "/opt/module/hadoop-3.3.4/etc/hadoop/workers"
        mv_file "/opt/module/hadoop-3.3.4/etc/hadoop/workers.bak" "/opt/module/hadoop-3.3.4/etc/hadoop/workers"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/sbin/stop-yarn.sh.bak" ]; then
        rm_file_dir "/opt/module/hadoop-3.3.4/sbin/stop-yarn.sh"
        mv_file "/opt/module/hadoop-3.3.4/sbin/stop-yarn.sh.bak" "/opt/module/hadoop-3.3.4/sbin/stop-yarn.sh"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/sbin/start-yarn.sh.bak" ]; then
        rm_file_dir "/opt/module/hadoop-3.3.4/sbin/start-yarn.sh"
        mv_file "/opt/module/hadoop-3.3.4/sbin/start-yarn.sh.bak" "/opt/module/hadoop-3.3.4/sbin/start-yarn.sh"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/sbin/stop-dfs.sh.bak" ]; then
        rm_file_dir "/opt/module/hadoop-3.3.4/sbin/stop-dfs.sh"
        mv_file "/opt/module/hadoop-3.3.4/sbin/stop-dfs.sh.bak" "/opt/module/hadoop-3.3.4/sbin/stop-dfs.sh"
    fi
    if [ -f "/opt/module/hadoop-3.3.4/sbin/start-dfs.sh.bak" ]; then
        rm_file_dir "/opt/module/hadoop-3.3.4/sbin/start-dfs.sh"
        mv_file "/opt/module/hadoop-3.3.4/sbin/start-dfs.sh.bak" "/opt/module/hadoop-3.3.4/sbin/start-dfs.sh"
    fi
    pkill -9 java
    log_title "清理并结束HADOOP服务搭建测试"
}

function hadoop_run() {
    local param="$1"
    local ser_ip="$2"
    local cli_ip="$3"
    sv_name_rpm "${param}"
    hadoop_build "${ser_ip}" "${cli_ip}"
    hadoop_test
}

###        Nginx_HTTPD服务       ###
function nginx_bak() {
    log_title "开始Nginx_HTTPD服务搭建测试"
}

function nginx_build() {
    rpm -qa | grep "nginx-[0-9]"
    title_assert "rpm -qa | grep nginx-[0-9]"
    stop_fw
    stop_se
    local nginx_pwd=$(pwd)
    local nginx_path="${nginx_pwd}/nginx"
    if [ -f "/etc/nginx/nginx.conf.bak" ]; then
        cp_file "${nginx_path}/nginx.conf" "/etc/nginx/"
    else
        mv_file "/etc/nginx/nginx.conf" "/etc/nginx/nginx.conf.bak"
        cp_file "${nginx_path}/nginx.conf" "/etc/nginx/"
    fi
    sed -i "s/10.60.3.191/${ip_b}/g" "/etc/nginx/nginx.conf"
    title_assert "sed -i 's/10.60.3.191/${ip_b}/g' /etc/nginx/nginx.conf"
    sed -i "s/10.60.3.199/${ip_c}/g" "/etc/nginx/nginx.conf"
    title_assert "sed -i 's/10.60.3.199/${ip_c}/g' /etc/nginx/nginx.conf"

    systemctl start nginx
    title_assert "systemctl start nginx"
    systemctl status nginx | grep "Active: active"
    title_assert "systemctl status nginx | grep 'Active: active'"
    ret_assert "NGINX服务端搭建"
}

function nginx_client() {
    rpm -qa | grep "^httpd-[0-9]"
    title_assert "rpm -qa|grep ^httpd-[0-9]"
    stop_fw
    stop_se
    local nginx_pwd=$(pwd)
    local nginx_path="${nginx_pwd}/nginx"
    if [ -f "/usr/share/httpd/noindex/index.html.bak" ]; then
        cp_file "${nginx_path}/index_${ser_ip}.html" "/usr/share/httpd/noindex/index.html"
    else
        mv_file "/usr/share/httpd/noindex/index.html" "/usr/share/httpd/noindex/index.html.bak"
        cp_file "${nginx_path}/index_${ser_ip}.html" "/usr/share/httpd/noindex/index.html"
    fi
    systemctl start httpd
    title_assert "systemctl start httpd"
    systemctl status httpd | grep "Active: active"
    title_assert "systemctl status httpd | grep 'Active: active'"
    ret_assert "NGINX服务端搭建"
}

function nginx_clean() {
    if [ -f "/etc/nginx/nginx.conf.bak" ]; then
        rm_file_dir "/etc/nginx/nginx.conf"
        mv_file "/etc/nginx/nginx.conf.bak" "/etc/nginx/nginx.conf"
    fi
    if [ -f "/usr/share/httpd/noindex/index.html.bak" ]; then
        rm_file_dir "/usr/share/httpd/noindex/index.html"
        mv_file "/usr/share/httpd/noindex/index.html.bak" "/usr/share/httpd/noindex/index.html"
    fi
    systemctl stop nginx
    title_assert "systemctl stop nginx"
    systemctl status nginx | grep "Active: inactive"
    title_assert "systemctl status nginx | grep 'Active: inactive'"
    systemctl stop httpd
    title_assert "systemctl stop httpd"
    systemctl status httpd | grep "Active: inactive"
    title_assert "systemctl status httpd | grep 'Active: inactive'"
}

function nginx_run() {
    local param="$1"
    local ip_b="$2"
    local ip_c="$3"
    sv_name_rpm "${param}"
    nginx_build "${ip_b}" "${ip_c}"
}

#运行指定服务项
function service_run() {
    local param="$1"
    local ser_ip="$2"
    local cli_ip="$3"
    git_lib
    case "${param}" in
    nfs)
        nfs_run "${param}"
        ;;
    http)
        http_run "${param}"
        ;;
    https)
        https_run "${param}"
        ;;
    ftp)
        ftp_run "${param}"
        ;;
    smb)
        smb_run "${param}"
        ;;
    ldap)
        ldap_run "${param}"
        ;;
    netconf)
        netconf_run "${param}"
        ;;
    restconf)
        restconf_run "${param}"
        ;;
    fastcgi)
        fastcgi_run "${param}"
        ;;
    snmp)
        snmp_run "${param}"
        ;;
    smtp)
        smtp_run "${param}"
        ;;
    ntp)
        ntp_run "${param}"
        ;;
    rbac)
        rbac_run "${param}"
        ;;
    rpc)
        rpc_run "${param}"
        ;;
    rsync)
        rsync_run "${param}"
        ;;
    ssh)
        ssh_run "${param}"
        ;;
    strongswan)
        strongswan_run "${param}" "${ser_ip}" "${cli_ip}"
        ;;
    ceph)
        ceph_run "${param}" "${ser_ip}" "${cli_ip}" "${dev_name}"
        ;;
    hadoop)
        hadoop_run "${param}" "${ser_ip}" "${cli_ip}"
        ;;
    nginx)
        nginx_run "${param}" "${ser_ip}" "${cli_ip}"
        ;;
    "")
        #ftp_run在https_run后执行会出错
        ldap_run "${param}"
        ftp_run "${param}"
        smb_run "${param}"
        nfs_run "${param}"
        https_run "${param}"
        http_run "${param}"
        netconf_run "${param}"
        restconf_run "${param}"
        fastcgi_run "${param}"
        snmp_run "${param}"
        smtp_run "${param}"
        ntp_run "${param}"
        rbac_run "${param}"
        rpc_run "${param}"
        rsync_run "${param}"
        ssh_run "${param}"
        strongswan_run "${param}" "${ser_ip}" "${cli_ip}"
        ceph_run "${param}" "${ser_ip}" "${cli_ip}" "${dev_name}"
        hadoop_run "${param}" "${ser_ip}" "${cli_ip}"
        nginx_run "${param}" "${ser_ip}" "${cli_ip}"
        ;;
    *)
        log_error "请检查输入的服务名是否正确"
        ;;
    esac
}

#清理指定服务项
function service_clean() {
    local param="$1"
    git_lib
    case "${param}" in
    nfs)
        nfs_clean
        ;;
    http)
        http_clean
        ;;
    https)
        https_clean
        ;;
    ftp)
        ftp_clean
        ;;
    smb)
        smb_clean
        ;;
    ldap)
        ldap_clean
        ;;
    netconf)
        netconf_clean
        ;;
    restconf)
        restconf_clean
        ;;
    fastcgi)
        fastcgi_clean
        ;;
    snmp)
        snmp_clean
        ;;
    smtp)
        smtp_clean
        ;;
    ntp)
        ntp_clean
        ;;
    rsync)
        rsync_clean
        ;;
    ssh)
        ssh_clean
        ;;
    strongswan)
        strongswan_clean
        ;;
    ceph)
        ceph_clean
        ;;
    hadoop)
        hadoop_clean
        ;;
    all)
        ldap_clean
        ftp_clean
        smb_clean
        nfs_clean
        https_clean
        http_clean
        netconf_clean
        restconf_clean
        fastcgi_clean
        snmp_clean
        smtp_clean
        ntp_clean
        rsync_clean
        ssh_clean
        strongswan_clean
        ceph_clean
        hadoop_clean
        ;;
    *)
        log_error "请检查输入的服务名是否正确"
        ;;
    esac
}

#测试指定服务项
function service_test() {
    local param="$1"
    git_lib
    case "${param}" in
    nfs)
        nfs_test
        ;;
    http)
        http_test
        ;;
    https)
        https_test
        ;;
    ftp)
        ftp_test
        ;;
    smb)
        smb_test
        ;;
    ldap)
        ldap_test
        ;;
    restconf)
        restconf_test
        ;;
    fastcgi)
        fastcgi_test
        ;;
    snmp)
        snmp_test
        ;;
    smtp)
        smtp_test
        ;;
    ntp)
        ntp_test
        ;;
    rbac)
        rbac_test
        ;;
    rpc)
        rpc_test
        ;;
    rsync)
        rsync_test
        ;;
    ssh)
        ssh_test
        ;;
    ceph)
        ceph_test
        ;;
    hadoop)
        hadoop_test
        ;;
    *)
        log_error "请检查输入的服务名是否正确"
        ;;
    esac
}

#客户端配置指定服务项
function service_client() {
    local param="$1"
    local ser_ip="$2"
    local cli_ip="$3"
    git_lib
    case "${param}" in
    ldap)
        ldap_client "${ser_ip}"
        ;;
    strongswan)
        strongswan_client "${ser_ip}" "${cli_ip}"
        ;;
    ceph)
        ceph_client "${ser_ip}" "${cli_ip}"
        ;;
    hadoop)
        hadoop_client "${ser_ip}" "${cli_ip}"
        ;;
    nginx)
        nginx_client "${ser_ip}"
        ;;
    *)
        log_error "请检查输入的服务名是否正确"
        ;;
    esac
}

main() {
    if [ "$#" -eq "0" ]; then
        service_run "$@"
    else
        #运行指定XML测试项
        while getopts "t:c:s:e:" opt; do
            case $opt in
            t)
                local sv_name=$OPTARG
                service_test "${sv_name}"
                ;;
            c)
                local sv_name=$OPTARG
                service_clean "${sv_name}"
                ;;
            s)
                local sv_name=$OPTARG
                local ser_ip="$3"
                local cli_ip="$4"
                local dev_name="$5"
                service_run "${sv_name}" "${ser_ip}" "${cli_ip}" "${dev_name}"
                ;;
            e)
                local sv_name=$OPTARG
                local ser_ip="$3"
                local cli_ip="$4"
                service_client "${sv_name}" "${ser_ip}" "${cli_ip}"
                ;;
            *)
                service_help
                exit 1
                ;;
            esac
        done
    fi
}

main $@
